package com.example.mybatisplus;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

public class MybatisPlusGenerator {

    private static final String AUTHOR = "AUTHOR_NAME";
    //工作路径
    private static final String PROJECT_PATH = "PROJECT_PATH";

    private static final String BASE = "com.example.mybatisplus";

    private static final String URL = "jdbc:mysql://localhost:3306/databaseName?useUnicode=true&useSSL=false&characterEncoding=utf-8&allowMultiQueries=true";

    private static final String USER_NAME = "";

    private static final String PASSWORD = "";

    public static void main(String[] args) {
        FastAutoGenerator.create(initDataSourceConfig())
                .globalConfig(MybatisPlusGenerator::initGlobalConfig)
                .packageConfig(MybatisPlusGenerator::initPackageConfig)
                .strategyConfig(MybatisPlusGenerator::initStrategyConfig)
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
//            .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

    /**
     * 初始化包配置
     */
    private static void initPackageConfig(Function<String, String> scanner, PackageConfig.Builder builder) {
        builder.parent(BASE)
                .moduleName(scanner.apply("请输入模块名:"))
//                .entity("po")
//                .service("service")
//                .serviceImpl("service.impl")
//                .mapper("mapper")
//                .xml("mapper.xml")
//                .controller("controller")
//                .other("other")
                .pathInfo(Collections.singletonMap(OutputFile.xml, PROJECT_PATH + "/src/main/resources/mapper/"));
    }
    /**
     * 初始化数据源
     */
    private static DataSourceConfig.Builder initDataSourceConfig() {
        return new DataSourceConfig.Builder(URL, USER_NAME, PASSWORD)
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler());
    }

    /**
     * 初始化策略配置
     */
    private static void initStrategyConfig(Function<String, String> scanner, StrategyConfig.Builder builder) {
        builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")));
    }
    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
    /**
     * 初始化公共配置
     */
    private static void initGlobalConfig(GlobalConfig.Builder builder) {
        builder.author(AUTHOR)
                .fileOverride()
                //swagger (默认false)
//                .enableSwagger()
                // 指定输出目录
                .outputDir(PROJECT_PATH + "/src/main/java")
                // 文件头时间
                .commentDate("yyyy-MM-dd HH:mm:ss")
                // 是否打开文件
                .disableOpenDir();
    }
}
